{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache C:\\Users\\Administrator\\AppData\\Local\\Temp\\jieba.cache\n",
      "Loading model cost 0.446 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "文本'今天天气晴朗，我非常开心地买了一束鲜艳的花'的情感得分： 6.19819897793\n",
      "文本'天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'的情感得分： -2.3225779827275\n"
     ]
    }
   ],
   "source": [
    "from collections import defaultdict\t\t#导入所需要的库与模块\n",
    "import jieba\n",
    "def cut_word(sentence):\t\t\t\t\t#分词并删除停用词\n",
    "\tcutWord = jieba.cut(sentence)\t\t\t#使用jieba进行分词\n",
    "\tcut_list = []\n",
    "\tfor i in cutWord:\t\t\t\t\t\t#将分词结果转换为列表\n",
    "\t\tcut_list.append(i)\n",
    "\tstopwords = set()\n",
    "\twith open('data/stopwords.txt', 'r', encoding = 'utf-8') as fr:\n",
    "\t\tfor i in fr:\n",
    "\t\t\tstopwords.add(i.strip())\n",
    "\t#删除停用词\n",
    "\treturn list(filter(lambda x :x not in stopwords, cut_list))\n",
    "#提取文本中的情感词、否定词和程度副词\n",
    "def classify_words(word_list):\n",
    "\tsen_file = open('data/BosonNLP_sentiment_score.txt', 'r+', \n",
    "encoding = 'utf-8')\t\t\t\t\t\t\t\t#读取情感词典文件\n",
    "\tsen_list = sen_file.readlines()\t\t#读取文件中每行的内容\n",
    "\tsen_dict = defaultdict()\t\t#实例化defaultdict类的对象\n",
    "\tfor i in sen_list:\n",
    "\t\tif len(i.split(' ')) == 2:\t\t\t#使用空格进行分割\n",
    "\t\t\t#将情感词作为字典的键，权值作为字典的值\n",
    "\t\t\tsen_dict[i.split(' ')[0]] = i.split(' ')[1]\n",
    "\t#读取否定词文件\n",
    "\tnot_word_file = open('data/否定词.txt', 'r+', encoding = 'utf-8')\n",
    "\tnot_word_list = not_word_file.readlines()\n",
    "\t#读取程度副词文件\n",
    "\tdegree_file = open('data/程度副词.txt', 'r+', encoding = 'utf-8')\n",
    "\tdegree_list = degree_file.readlines()\n",
    "\tdegree_dict = defaultdict()\n",
    "\tfor i in degree_list:\n",
    "\t\tdegree_dict[i.split(',')[0]] = i.split(',')[1]\n",
    "\t#创建3个空字典，分别用于存储情感词、否定词和程度副词\n",
    "\tsen_word = dict()\n",
    "\tnot_word = dict()\n",
    "\tdegree_word = dict()\n",
    "\tfor i in range(len(word_list)):\n",
    "\t\tword = word_list[i]\n",
    "\t\tif word in sen_dict.keys() and word not in not_word_list and word not in degree_dict.keys():\t\t\t#判断词是否在情感词典中\n",
    "\t\t\tsen_word[i] = sen_dict[word]\n",
    "\t\telif word in not_word_list and word not in degree_dict.keys():\t\t\t\t\t\t\t\t#判断词是否在否定词列表中\n",
    "\t\t\tnot_word[i] = -1\n",
    "\t\t#判断词是否在程度副词字典中\n",
    "\t\telif word in degree_dict.keys():\n",
    "\t\t\tdegree_word[i] = degree_dict[word]\n",
    "\treturn sen_word, not_word, degree_word\n",
    "#计算情感得分\n",
    "def count_score(sen_word, not_word, degree_word, seg_result):\n",
    "\tw = 1\t\t\t\t\t\t\t\t\t\t#权重初始化为1\n",
    "\tscore = 0\n",
    "\tsen_index = -1\t\t\t\t\t\t\t#情感词下标初始化\n",
    "\t#情感词的位置下标集合\n",
    "\tsenIndex_list = list(sen_word.keys())\n",
    "\tfor i in range(0, len(seg_result)):\t#遍历分词结果\n",
    "\t\tif i in sen_word.keys():\t\t\t#如果是情感词\n",
    "\t\t\t#权重与情感词得分相乘\n",
    "\t\t\tscore += w * float(sen_word[i])\n",
    "\t\t\tsen_index += 1\n",
    "\t\t\tif sen_index < len(senIndex_list)-1:\n",
    "\t\t\t\t#判断当前的情感词与下一个情感词之间是否有程度副词或否定词\n",
    "\t\t\t\tfor j in range(senIndex_list[sen_index], \n",
    "senIndex_list[sen_index + 1]):\n",
    "\t\t\t\t\tif j in not_word.keys():  #如果有否定词，权重取反\n",
    "\t\t\t\t\t\tw *= -1\n",
    "\t\t\t\t\t#如果有程度副词，权重与程度副词的权重相乘\n",
    "\t\t\t\t\telif j in degree_word.keys():\n",
    "\t\t\t\t\t\tw *= float(degree_word[j])\n",
    "\treturn score\n",
    "#计算文本的情感得分\n",
    "def sentiment_score(sentence):\n",
    "\tseg_list = cut_word(sentence)\t\t\t\t#分词\n",
    "\t#调用classify_words()函数，提取情感词、否定词和程度副词\n",
    "\tsen_word, not_word, degree_word = classify_words(seg_list)\n",
    "\t#调用count_score()函数，计算情感得分\n",
    "\tscore = count_score(sen_word, not_word, degree_word, seg_list)\n",
    "\treturn score\n",
    "if __name__ == '__main__':\n",
    "\tprint(\"文本'今天天气晴朗，我非常开心地买了一束鲜艳的花'的情感得分：\", sentiment_score('今天天气晴朗，我非常开心地买了一束鲜艳的花'))\n",
    "\tprint(\"文本'天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'的情感得分：\", sentiment_score('天阴沉沉的，荒无人烟的路边是破旧不堪的老房子'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
